package com.gxlu.ms.base.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 *
 * @author lhc
 * @date 2018/1/26
 */
public class StringUtil {

    public static final String SEPERATOR = ",";

    /**
     * 判断一个对象是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        return null == obj || obj.toString().length() == 0;
    }

    /**
     * 判断一个对象是否不为空
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        return obj != null && obj.toString().length() > 0;
    }

    /**
     * 返回对象的字符串值，null则返回空串
     *
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        return null == obj ? "" : obj.toString();
    }

    /**
     * 用默认分隔符拼接字符串数组
     *
     * @param strArr
     * @return
     */
    public static String concatString(String... strArr) {
        if (ArrayUtils.isEmpty(strArr)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String str : strArr) {
            if (StringUtils.isEmpty(str)) {
                continue;
            }
            sb.append(str);
            sb.append(SEPERATOR);
        }
        return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "";
    }

    /**
     * 将字符串转换为列表
     *
     * @param str
     * @param seperator
     * @return
     */
    public static List<String> stringToList(String str, String seperator) {
        List<String> list = new ArrayList<String>();
        if (StringUtils.isEmpty(str)) {
            return list;
        }
        String[] arr = str.split(seperator);
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
        return list;
    }

    /**
     * 单词的首字母大写
     *
     * @param str
     * @return
     */
    public static String upperCaseFirstLatter(String str) {
        char[] chars = str.toCharArray();
        chars[0] -= 32;
        return String.valueOf(chars);
    }

    /**
     * 单词的首字母转小写
     *
     * @param str
     * @return
     */
    public static String lowerCaseFirstLetter(String str) {
        char[] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    /**
     * 下划线拼接的单词转为驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToCamel(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        int len = str.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (c == '_') {
                if (++i < len) {
                    sb.append(Character.toUpperCase(str.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰拼接的单词转为下划线
     *
     * @param str
     * @return
     */
    public static String camelToUnderline(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        int len = str.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_");
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }


}
